#include "rts_message_receiver.hpp"

#include <boost/foreach.hpp>
#include <iostream>

#include "rts_server.hpp"
#include "rts_client.hpp"

rts_message_receiver::rts_message_receiver()
{
}

void rts_message_receiver::receive_message(const std::wstring& message, boost::shared_ptr<client> sender)
{
    rts_server* server = static_cast<rts_server*>(sender->server());
    switch(message[0])
    {
        case game::initial_game_state:
        {
            if(server->clients().size() == 2)
            {
                std::vector<boost::shared_ptr<rts_client> > players;
                BOOST_FOREACH(client_map::value_type& c, sender->server()->clients())
                {
                    players.push_back(boost::static_pointer_cast<rts_client>(c.second));
                }
                server->start_game(players);
            }
            break;
        }
        default:
        {
            BOOST_FOREACH(client_map::value_type& c, sender->server()->clients())
            {
                c.second->send(message);
            }
            break;
        }
    }
}
